import Vue from 'vue'
import Vuex from 'vuex'

import { UPDATE_INFO , DECREMENT } from "./mutations";

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0 ,
    user:{
      name : 'houzi2',
      age : 18 ,
      sex : '男'
    }
  },
  mutations: {
    [UPDATE_INFO] (state) {
      state.count++
    },

    [DECREMENT] (state , payload) {
      // console.log(state);
      // console.log(payload);
      // state.count-=payload
      setTimeout(()=>{
        state.count-=payload
      } , 2000 )
    },
    modifyUser(state){
      // 错误代码：mutation 中不做异步操作
      // setTimeout(()=>{
      //   state.user.name = 'houzi333'
      //   console.log(state.user);
      // } , 2000 )
      state.user.name = 'houzi333'
    }

  },
  actions : {
    asyncModifyUser({commit}){

      setTimeout(()=>{
        commit('modifyUser')
      })
    }
  }
})

export default  store ;
